<?php

Loader::using("models::entities::Solicitudes");
Loader::using("models::entities::enums::EstadoSolicitud");

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of VerSolicitudes
 *
 * @author ETorres
 * export-metadata RequiredRoles:ROLE_PRACTICES_COORDINATOR
 * 
 * @export Modules
 */
class VerSolicitudes extends Module {
    
    public function index() {
        $Solicitudes = $this->model->applications->GetAll();
        
        $EstadoSolicitud = EstadoSolicitud::values();

        $this->assign("solicitudes", $Solicitudes);
        $this->assign("verSolicitud", $this->route->action("VerSolicitud"));
        $this->assign("asignarPracticante", $this->route->moduleAction("ResponderSolicitud", "index"));
        $this->assign("rechazarSolicitud", $this->route->action("RechazarSolicitud"));
        $this->assign("verBitacora", $this->route->action("verBitacora"));
        $this->assign("estadosSolicitud", $EstadoSolicitud);
        $this->display("index");
    }
    
    public function VerSolicitud() {
        $id = $this->request->get("idSolicitud", NULL);
        if(is_numeric($id)){
            
            $solicitud = new Solicitudes();
            $solicitud = $this->model->applications->GetById($id);
          
            if(!is_null($solicitud->getPracticanteActual())){
            $solicitud->getPracticanteActual()->setEstudiante($this->model->students->GetById($solicitud->getPracticanteActual()->getCodigoEstudiante()));
            }
          
            $this->assign("solicitud", $solicitud);
            $this->assign("UrlVolver", $this->route->action("index"));
            $this->display(); 
        }else{
            $this->index();
        }
            
    }
    
    public function RechazarSolicitud() {
        $id = $this->request->get("idSolicitud", NULL);
        if(is_numeric($id)){
           
            $solicitud = $this->model->applications->GetById($id);
            
            $solicitud->setEstado(EstadoSolicitud::Rechazada);
            
            $this->model->applications->Update($solicitud);
            
            $this->addMessage(new Message("", "Solicitud Rechazada con Exito!!!", Message::SUCCESS));
            
            $this->index();

        }else{
            
            $this->addMessage(new Message("", "Id de la solicitud no valida...", Message::ERROR));
            
            $this->index();
        }
    }


    public function SeleccionarPracticante() {

        $this->Solicitud->setEstado(EstadoSolicitud::Procesada);
        $this->Solicitud->getPracticanteActual()->setIdSolicitud($this->Solicitud->getIdSolicitud());
        $this->Solicitud->getPracticanteActual()->setIsRemunerado($this->Solicitud->getPracticanteActual()->getRemuneracion() > 0);

        $this->model->applications->Update($this->Solicitud);

        $this->display();
    }

    private $Solicitud;

    public function getSolicitud() {

        if (!$this->Solicitud) {
            $this->Solicitud = $this->model->applications->GetById($this->request->get("SolicitudId"));
            Loader::using("models::entities::Practicantes");
            $solicitud->setPracticanteActual(new Practicantes());
        }
        
        return $this->Solicitud;
    }
    
    public function VerBitacora() {
        
        $Practicante=$this->model->practitioners->GetByEstudiante($this->currentUser->getName());
        $bitacoras=$this->model->logbooks->GetByPracticante($Practicante->getIdPracticante());
       
        $this->assign("practicante", $Practicante);
        $this->assign("bitacoras", $bitacoras);
        
        $this->display();
        
    }
    
}

?>
